<?php
/**
 +------------------------------------------------------------------------------
 * Eorder订单后期编辑操作
 +------------------------------------------------------------------------------
 * @category   SubAction
 * @package  app
 * @subpackage  Action
 * @author   朝闻道 <admin@iic.cn>
 * @date 2013-5-13
 * @time  下午4:42:38
 * @功能 修改财务信息（缺货、退款）
 * @功能 修改订单付款状态（O2O时有效）
 * @功能 修改订单的发货状态（发货快递号）
 * @功能 修改发货时扫描的条形码
 * 
 +------------------------------------------------------------------------------
 */
class Eorder {
	protected $line = '';
	public $err91yungou = 0;
	/**
	 * 修改财务信息 财务信息只能添加，不能删除及修改
	 * 附加财务操作时新写入状态（status）一律为0，扣款成功后再次修改为1
	 * 
	 * @param array $finance=array('order_id'=>1,'action'=>'BUY','goods_id'=>'',
	 *        	'number'=>'','remark'=>'','add_ip'=>'','code'=>'','platform'=>'91YUNGOU',
	 *        	'subject'=>'CNY','value'=>'10000')
	 * @param
	 *        	参数 action BUY购买 OOS缺货 REFUND退款 LARGESS赠送 FREEZE冻结
	 * @param
	 *        	参数 subject CNY BV CV MV PV 目前O2O只有CNY
	 * @param
	 *        	参数 value 单位是分 100CV=10000分,100MV=1000分,BV=CV,1PV=1分
	 * @return int $id
	 *         @date 2013-5-14
	 *         @time 下午2:14:18
	 */
	function e_finance($finance = array()) {
		// 修改财务信息
		$this->line = 'e_finance::' . __LINE__;
		if (empty ( $finance )) {
			$err = array (
					"status" => 0,
					'msg' => '财务信息写入不成功，参数不能为空。' 
			);
			return self::_err ( $err );
		}
		if (empty ( $finance ['userid'] )) {
			$err = array (
					"status" => 0,
					'msg' => '财务信息写入不成功，用户ID不能为空。' 
			);
			return self::_err ( $err );
		}
		if (empty ( $finance ['order_id'] )) {
			$err = array (
					"status" => 0,
					'msg' => '财务信息写入不成功，订单ID不能为空。' 
			);
			return self::_err ( $err );
		}
		if (($finance ['action'] == 'OOS' || $finance ['action'] == 'REFUND') && (empty ( $finance ['goods_id'] ) || empty ( $finance ['number'] ))) {
			$err = array (
					"status" => 0,
					'msg' => '财务信息写入不成功，缺货与退款操作中商品及数量不能为空。' 
			);
			return self::_err ( $err );
		}
		$financedao = M ( 'OrderFinance' );
		$data = array ();
		$data ['userid'] = $finance ['userid'];
		$data ['action_userid'] = $finance ['action_userid'];
		$data ['order_id'] = $finance ['order_id'];
		$data ['action'] = $finance ['action']; //
		$data ['goods_id'] = $finance ['goods_id']; // 只有退款时有关联产品ID
		$data ['number'] = $finance ['number'];
		$data ['remark'] = $finance ['remark'];
		$data ['cip'] = $finance ['add_ip']?$finance ['add_ip']:get_client_ip();
		$data ['code'] = $finance ['code']?$finance ['code']:0;
		$data ['status'] = '0'; // 附加财务操作时新写入状态一律为0，扣款成功后再次修改为1
		$data ['ctime'] = time ();
		$data ['platform'] = $finance ['platform'];
		$data ['subject'] = $finance ['subject'];
		$data ['value'] = $finance ['value'];
		$err = array ();
		$id = $financedao->add ( $data );
		if (empty ( $id )) {
			$debug = $financedao->getLastSql ();
			$err = $data;
		}
		if (! empty ( $err )) {
			$err = array (
					"status" => 0,
					'msg' => '财务信息写入出现错误。'.__LINE__,
					'data' => $err,
					'debug' => $debug 
			);
			return self::_err ( $err );
		}
		return $id;
	} // end e_finance
	
	/**
	 * 修改财务记录状态
	 * @date 2013-5-18
	 * @time 下午3:04:57
	 * 
	 * @param array $arr=array('id'=>'','status'=>'')        	
	 */
	function e_finance_status($arr = array()) {
		// 修改财务记录状态
		$this->line = 'e_finance_status::' . __LINE__;
		if (empty ( $arr ) || !is_array ( $arr )) {
			$err = array (
					"status" => 0,
					'msg' => '财务记录状态修改不成功，参数不能为空。' 
			);
			return self::_err ( $err );
		}
		$financedao = M ( 'OrderFinance' );
		$err = false;
		$debug = array ();
		foreach ( $arr as $v ) {
			$data = array ();
			$data ['id'] = $v ['id'];
			$data ['status'] = $v ['status'];
			$res = $financedao->save ( $data );
			if ($res === false) {
				$debug [] = $financedao->getLastSql ();
				$err = true;
			}
		}
		if ($err) {
			$err = array (
					"status" => 0,
					'msg' => '财务记录状态修改出现错误,请检查。',
					'debug' => $debug 
			);
			return self::_err ( $err );
		}
		return true;
	} // end e_finance_status
	
	/**
	 * 修改订单的付款状态
	 * @date 2013-5-14
	 * @time 下午2:25:53
	 * 
	 * @param array $arr=array('orderid'=>1,'pay_status'=>1,'userid'=>1,'cip'=>'xx.xx.xx.xx')
	 *        	订单及支付状态
	 *        	
	 */
	function e_pay_status($arr = array()) {
		// 修改订单的付款状态
		$this->line = 'e_pay_status' . __LINE__;
		if (empty ( $arr )) {
			$err = array (
					"status" => 0,
					'msg' => '订单状态修改不成功，参数不能为空。' 
			);
			return self::_err ( $err );
		}
		if (empty ( $arr ['orderid'] )) {
			$err = array (
					"status" => 0,
					'msg' => '订单状态修改不成功，订单号不能为空。' 
			);
			return self::_err ( $err );
		}
		if (empty ( $arr ['pay_status'] ) && $arr ['pay_status'] != '0') {
			$err = array (
					"status" => 0,
					'msg' => '订单状态修改不成功，修改状态不能为空。' 
			);
			return self::_err ( $err );
		}
		$order = M ( 'Order' );
		$data = array ();
		$data ['id'] = $arr ['orderid'];
		$data ['pay_status'] = $arr ['pay_status'];
		$res = $order->save ( $data );
		if ($res === false) {
			$err = array (
					"status" => 0,
					'msg' => '订单支付状态修改不成功，操作异常。' 
			);
			return self::_err ( $err );
		}
		return true;
	} // end e_pay_status
	
	/**
	 * 修改订单的状态
	 * @date 2013-5-17
	 * @time 下午2:58:56
	 */
	function e_status($order = 0, $status = 0) {
		// 修改订单的状态
		$this->line = 'e_status::' . __LINE__;
		if (empty ( $order ) || $order == '0') {
			$err = array (
					"status" => 0,
					'msg' => '订单状态修改不成功，订单ID不能为空。' 
			);
			return self::_err ( $err );
		}
		$order = M ( 'Order' );
		$data = array ();
		$data ['id'] = $order;
		$info = $order->where ( $data )->find ();
		if (empty ( $info )) {
			$err = array (
					"status" => 0,
					'msg' => '订单支付状态修改不成功，没有找到相关的订单信息。' 
			);
			return self::_err ( $err );
		}
		if ($status < $info ['status']) {
			$err = array (
					"status" => 0,
					'msg' => '订单支付状态修改不成功，订单的状态信息不能逆转。' 
			);
			return self::_err ( $err );
		}
		$data ['status'] = $status;
		$res = $order->save ( $data );
		if ($res === false) {
			$err = array (
					"status" => 0,
					'msg' => '订单支付状态修改不成功，操作异常。' 
			);
			return self::_err ( $err );
		}
		return true;
	} // end e_status
	
	/**
	 * 91云够的错误输出
	 * @date 2013-6-8
	 * @time 下午10:26:43
	 */
	function err91yungou($str = '') {
		// 91云够的错误输出
		exit ( $str );
	} // end err91yungou
	
	/**
	 * 输出错误信息
	 * @date 2013-5-6
	 * @time 下午5:19:24
	 */
	function _err($arr = array(), $str = "操作出现错误。") {
		// 请求接口类型异常
		if (is_array ( $arr ) && ! empty ( $arr )) {
			$res = array (
					"status" => 0,
					"msg" => $str,
					'data' => $arr,
					'line' => $this->line
			);
		} else {
			$res = array (
					"status" => 0,
					"msg" => $str,
					'data' => $this->debug,
					'line' => $this->line
			);
		}
		$res = json_encode ( $res );
		if ($this->err91yungou == 1) {
			self::err91yungou ( $res );
		}
		return $res;
	} // end _err
	
	/**
	 * 请求成功返回
	 * @date 2011-11-15
	 * @time 上午08:10:19
	 */
	private function _suc($arr = array(), $str = "操作成功") {
		// 请求接口类型异常
		$res = array (
				"status" => 1,
				"msg" => $str,
				'data' => $this->debug
		);
		if (! empty ( $arr )) {
			$res = array_merge ( $res, $arr );
		}
		echo json_encode( $res );
		die ();
	} // end _suc
}//end Eorder

